package view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;

import common.Contexto;
import controller.MaterialController;
import model.Material;

@SuppressWarnings("serial")
public class ListaMaterialesPanel extends JPanel {

	private JButton modificarB, volverB;
	private JScrollPane materialesSP;
	private JTable materialesT;
	private ModificarMaterialesTableModel tableModel;
	
	public ListaMaterialesPanel(List<Material> materiales) {
		this.tableModel = new ModificarMaterialesTableModel(materiales);
		initGui();
		layoutComponents();
		initActions();
	}

	private void initGui() {
		volverB = new JButton("Volver");
		modificarB = new JButton("Modificar");

		materialesT = new JTable(tableModel);
		materialesT.setPreferredScrollableViewportSize(new Dimension(300,200));
		materialesSP = new JScrollPane(materialesT);
	}

	

	private void layoutComponents() {
		this.setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 5;
		c.ipadx = 180;
		c.ipady = 100;
		c.fill = GridBagConstraints.BOTH;
		add(materialesSP, c);
		
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.ipady = 0;
		c.insets = new Insets(20, 0, 20, 0);

		
		add(modificarB, c);
		c.gridx = 2;
		add(volverB, c);

	}

	private void initActions() {
		modificarB.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Material material = getMaterialSeleccionado();
				if(material != null) {
					MaterialController.getInstance().mostrarMaterial(material.getCodigo());
					SwingUtilities.windowForComponent(ListaMaterialesPanel.this).dispose();
				} else {
					notificarError("Por favor seleccione un material del listado");
				}
			}
		});

		volverB.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				SwingUtilities.windowForComponent(ListaMaterialesPanel.this).dispose();
			}
		});
		
	}
	
	private Material getMaterialSeleccionado() {
		return tableModel.getMaterialSeleccionado();
	}
	
	public void showPanel() {
		final JFrame mainFrame = new JFrame();
		mainFrame.setTitle("Materiales");
		mainFrame.getContentPane().add(this);
		mainFrame.setPreferredSize(new Dimension(600, 450));
		mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		mainFrame.pack();
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setVisible(true);
	}
	
	private void notificarError(String error) {
		Contexto.displayErrorMessage(ListaMaterialesPanel.this,error);
	}
	
	public void actualizarMateriales(List<Material> materiales) {
		tableModel.setMateriales(materiales);
	}

}